﻿using AutoMapper.Internal.Mappers;
using ERP.ERPSystem.ProductionSystem;
using ERP.ERPSystem.ProductionSystem.Dto;
using ERP.Team.ProductionSystem;
using Humanizer;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace ERP.Web.Controllers.ProductionSystem
{
    public class DeviceledgerController : Controller
    {
        private readonly IDeviceledgerService _deviceledgerService;

        public DeviceledgerController(IDeviceledgerService deviceledgerService)
        {
            _deviceledgerService = deviceledgerService;
        }

        public IActionResult DeviceledgerQuery()
        {
            return View();
        }

        /// <summary>
        /// 添加设备台账
        /// </summary>
        /// <param name="deviceledgerDTO"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<int> AddDeviceledger(DeviceledgerDTO deviceledgerDTO)
        {
            var diviceledger=_deviceledgerService.AddDeviceledger(deviceledgerDTO);
            if (diviceledger.Result == 0)
            {
                throw new Exception("添加设备台账失败");
            }
            return diviceledger.Result;
        }
        
        /// <summary>
        /// 更新设备台账
        /// </summary>
        /// <param name="deviceledgerDTO"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<int> UpdateDeviceledger(DeviceledgerDTO deviceledgerDTO)
        {
            var result = await _deviceledgerService.UpdateDeviceledger(deviceledgerDTO);
            if (result == 0)
            {
                throw new Exception("更新设备台账失败");
            }
            return result;
        }
        
        /// <summary>
        /// 删除设备台账
        /// </summary>
        /// <param name="id">设备ID</param>
        /// <returns></returns>
        [HttpPost]
        public async Task<int> DeleteDeviceledger(int id)
        {
            var result = await _deviceledgerService.DeleteDeviceledger(id);
            if (result == 0)
            {
                throw new Exception("删除设备台账失败，设备不存在或已被删除");
            }
            return result;
        }
        
        /// <summary>
        /// 批量删除设备台账
        /// </summary>
        /// <param name="ids">设备ID列表</param>
        /// <returns></returns>
        [HttpPost]
        public async Task<int> DeleteDeviceledgerBatch([FromBody] List<int> ids)
        {
            var result = await _deviceledgerService.DeleteDeviceledgerBatch(ids);
            if (result == 0)
            {
                throw new Exception("批量删除设备台账失败，没有找到要删除的设备");
            }
            return result;
        }
        
        /// <summary>
        /// 设备台账列表展示
        /// </summary>
        /// <param name="code">设备编码</param>
        /// <param name="name">设备名称</param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        [HttpGet]
        public async Task<List<DeviceledgerModel>> GetDeviceledgerModel(string? code, string? name)
        {
           var deviceledgerModels = _deviceledgerService.GetDeviceledgerModel(code, name);
            if (deviceledgerModels.Result == null)
            {
                throw new Exception("获取设备台账信息失败");
            }
            return deviceledgerModels.Result;
        }
    }
}
